package com.food.eat.util;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;

/**
 * @Author RenPu
 * @Date 2022/6/27 17:05
 * @Version 1.0
 * @Description: Vo对象的实体设计
 **/

@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class ApiResult<T> implements Serializable {

    private final String serializableID="411731814484355577L";

    private Integer responseCode; // 响应编码
    private String responseMsg;   //响应信息
    private Boolean isSuccess;    //是否成功
    private T data;               //具体的响应数据



    public static ApiResult<String> success(){
        return success("success");
    }

    public static <T> ApiResult<T> success(T data){
        ApiResult<Object> Vodata = ApiResult.builder().
                                   responseCode(200).
                                   responseMsg("恭喜操作成功！！！！！！").
                                   isSuccess(true).
                                   data(data).
                                   build();



        return (ApiResult<T>) Vodata;
    }


    public static <T> ApiResult<T> success(int code, String msg, T data){
        ApiResult<Object> Vodata = ApiResult.builder().
                responseCode(code).
                responseMsg(msg).
                isSuccess(true).
                data(data).
                build();

        return (ApiResult<T>) Vodata;
    }



    protected static ApiResult<String> fail(){
        return fail(-1);
    }

    protected static ApiResult<String> fail(int i) {
        return fail(i,"fail");

    }


    protected static ApiResult<String> fail(int i, String msg) {
        ApiResult<Object> Vodata = ApiResult.builder().
                responseCode(i).
                responseMsg(msg).
                isSuccess(false).
                data(null).
                build();
        return new ApiResult<>();
    }


    protected static <T> ApiResult<T> fail(T data) {
        return fail(-1,data);
    }

    private static <T> ApiResult<T> fail(int i, T data) {
        ApiResult<Object> Vodata = ApiResult.builder().
                responseCode(i).
                responseMsg("操作失败。。。。。。").
                isSuccess(false).
                data(data).
                build();
        return new ApiResult<>();
    }


    private static <T> ApiResult<T> fail(int code, String msg, T data){
        ApiResult<Object> Vodata = ApiResult.builder().
                responseCode(code).
                responseMsg(msg).
                isSuccess(false).
                data(data).
                build();
        return new ApiResult<>();

    }




    @Override
    public String toString() {
        return "ApiResult{" +
                "responseCode=" + responseCode +
                ", responseMsg='" + responseMsg + '\'' +
                '}';
    }
}
